package com.itheima.question.controller;

import com.itheima.Result;
import com.itheima.common.contants.RedisPicConstans;
import com.itheima.question.config.FastDFSClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/question")
@Api(value = "文件上传模块", tags = "upload", description = "用于实现各种文件上传")
public class FileUploadController {

    @Autowired
    private FastDFSClientUtil clientUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("{fileServerUrl}")
    private String fileServeUrl;

    @PostMapping("/upload")
    @ApiOperation("上传图片文件")
    public Result upload(MultipartFile file){

        try {
            String fileId = clientUtil.uploadFile(file);
            redisTemplate.opsForSet().add(RedisPicConstans.ALL_PIC,fileId);
            return new Result(true,"上传图片成功",fileServeUrl+fileId);
        }catch (IOException e){
            e.printStackTrace();
            return new Result(false,"上传图片失败",null);
        }

    }

}
